// save-order.js
var app = getApp();
const scan = require('../../utils/scanCode')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        shoppingCart: {},
        people: 0,
        storeId: '',
        storeType: '',
        seatId: 0,
        memo: '',
        btnName: '保存菜单',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let shoppingCart = wx.getStorageSync('shoppingCart');
        var currentShoppingCart = JSON.parse(shoppingCart)
        this.setData({
            storeId: options.storeId,
            storeType: options.storeType,
            shoppingCart: currentShoppingCart
        })
        console.log(this.data.shoppingCart)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var info = scan.getScanQrcode()

        if (info['mid'] == this.data.storeId && info['seat_id']) {
            this.setData({
                btnName:"添加"
            })
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },


    /**
     * 添加用餐人数
     */
    addPeople: function () {
        var peopleNum = this.data.people + 1
        this.setData({
            people: peopleNum
        })
    },
    /**
     * 减少用餐人数
     */
    minusPeople: function () {
        if (parseInt(this.data.people) > 1) {
            var peopleNum = parseInt(this.data.people) - 1
            this.setData({
                people: peopleNum
            })
        }
        else {
            wx.showModal({
                title: '提示',
                content: '就餐人数不能少于1',
                showCancel: false
            })
        }
    },
    isScanShop: function(){
        var info = scan.getScanQrcode()
        if (info['mid'] == this.data.storeId && info['seat_id']) {
            return 1
        }
        return 0;

    },
    /**
     * 提交订单
     */
    submit: function () {
        var path = '/api/v1/user_product/save?';
        var info = scan.getScanQrcode()
        if (info['mid'] == this.data.storeId && info['seat_id']) {
            this.data.seatId = info['seat_id']
            var path = '/api/v1/menu/save?';
        }
        this.data.people = 0
        var product = ''
        for (var i = 0; i < this.data.shoppingCart.list.length; i++) {
            if (parseInt(this.data.shoppingCart.list[i].num) > 0) {
                product += 'id=' + this.data.shoppingCart.list[i].data.id + '&qty=' + this.data.shoppingCart.list[i].num + ','
            }
        }
        path += 'merchant_id=' + this.data.storeId + '&people=' + this.data.people + '&product=' + encodeURIComponent(product) + '&memo=' + this.data.memo
            + '&seat_id=' + this.data.seatId
        var that = this;
        getApp().http.request({
            url: path, //仅为示例，并非真实的接口地址
            data: {},
            success: function (res) {
                console.log(res)
                if (res.data.code == '200') {
                    console.log(res.data.data)
                    app.globalData.in_shopping_cart = {}
                    setTimeout(function () {
                        wx.showToast({
                            title: '订单保存成功',
                            duration:1000
                        })
                    }, 1000)
                }
                // //订单保存成功后清理购物车
                var userShoppingCart = getApp().globalData.shoppingCart
                //订单保存成功后清理购物车
                for (var i = 0; i < userShoppingCart.length; i++) {
                    if (that.data.storeId == userShoppingCart[i].mid) {
                        getApp().cleanShoppingCart(that.data.storeId, '点餐')
                    }
                }
                if (that.isScanShop()) {
                    console.log('goseat')
                    let newVar = {url: "/pages/eating/eating"};
                    wx.switchTab(newVar)
                } else {
                    wx.navigateBack({
                        delta: 2
                    })
                }
            },
            fail: function () {

            }
        })
    },
    /**
     * 备注信息
     */
    memoEvent: function (event) {
        this.setData({
            memo: event.detail.value
        })
    },
    /**
     * 添加商品进入购物车事件
     */
    addEvent: function (event) {
        console.log(event)
        if (this.data.storeType == "点餐") {
            var tmp_shoppingCart = this.data.shoppingCart
            tmp_shoppingCart.list[event.currentTarget.dataset.index].num += 1
            tmp_shoppingCart.allPrice += tmp_shoppingCart.list[event.currentTarget.dataset.index].data.price
            tmp_shoppingCart.count += 1
            this.setData({
                shoppingCart: tmp_shoppingCart
            })
        }
    },
    /**
     * 从购物车删除商品事件
     */
    minusEvent: function (event) {

        if (this.data.storeType == "点餐") {
            var tmp_shoppingCart = this.data.shoppingCart
            console.log(tmp_shoppingCart.list)
            if (tmp_shoppingCart.list[event.currentTarget.dataset.index].num > 1) {
                tmp_shoppingCart.list[event.currentTarget.dataset.index].num -= 1
                tmp_shoppingCart.allPrice -= tmp_shoppingCart.list[event.currentTarget.dataset.index].data.price
                tmp_shoppingCart.count -= 1
                this.setData({
                    shoppingCart: tmp_shoppingCart
                })
            }
            else {
                wx.showModal({
                    title: '提示',
                    content: '单品数量不能少于1',
                    showCancel: false
                })
            }
        }
    },

    checkWord: function (e) {
      if (e.detail.value.length == 5) {
        var dv = document.createElement('view');
        dv.innerHTML = o.value;
        document.getElementById('words').appendChild(dv);
        o.value = '';
      }
    }
})